package com.xwj.controller.admin;

import com.xwj.entity.RaMenuInfo;
import com.xwj.service.Impl.RaMenuServiceImpl;
import com.xwj.service.Impl.RaRootServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xuweijie on 2018/1/14.
 */
@RestController
@RequestMapping("/admin/menu/")
public class MenuController {

    @Autowired
    private RaMenuServiceImpl raMenuService;
    @Autowired
    private RaRootServiceImpl raRootService;

    @RequestMapping("menulist")
    public ModelAndView menuList(ModelAndView modelAndView) {
        modelAndView.setViewName("/admin/menu/menu");
        modelAndView.addObject("menus", raMenuService.findItemMenu());
        modelAndView.addObject("roots", raRootService.findAllRoot());
        return modelAndView;
    }

    @RequestMapping(value = "updateMenu", method = RequestMethod.POST)
    public Map<String, String> updateMenu(RaMenuInfo raMenuInfo) {
        Map<String, String> response = new HashMap<>();
        if (raMenuService.updateMenu(raMenuInfo) > 0)
            response.put("msg", "更新成功");
        else
            response.put("msg", "更新失败，请重新操作");
        return response;
    }

    @RequestMapping(value = "deleteMenu", method = RequestMethod.POST)
    public Map<String, String> deleteMenu(@RequestParam("menuId") int id) {
        Map<String, String> response = new HashMap<>();
        if (raMenuService.deleteMenu(id) > 0)
            response.put("msg", "删除成功");
        else
            response.put("msg", "删除失败，请重新操作");
        return response;
    }

    @RequestMapping(value = "addMenu", method = RequestMethod.POST)
    public Map<String, String> addMenu(RaMenuInfo raMenuInfo) {
        Map<String, String> response = new HashMap<>();
        if (raMenuService.addMenuByMenuInfo(raMenuInfo) > 0)
            response.put("msg", "添加成功");
        else
            response.put("msg", "添加失败，请重新操作");
        return response;
    }

}
